// miniprogram/pages/search/search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchword: '',
    text: {},
    f: false,
    fview: false,
    youkey: '',
    showtext: '',
  },
  //点击解密
  handClick: function () {

    if (this.data.text.longtext&&(!this.data.f)) {
      this.setData({
        youkey:'',
        fview: true,
      })
    }
    if (!this.data.text.longtext) {
      wx.showToast({
        title: '没有内容要解密，请重新查询！',
        icon: 'none',
      })
    }
    if (this.data.f) {
      wx.showToast({
        title: '已解密!',
      })
    }
  },

  //取输入的key值
  putkey: function (e) {
    this.setData({
      youkey: e.detail.value
    })
  },

  //弹出框点确定
  gotext: function () {

    if (this.data.youkey.replace(/(^\s*)|(\s*$)/g, "") == '') {
      wx.showToast({
        title: 'key不能为空！',
        icon: 'none',
      })
      return;
    }
    this.setData({
      fview: false,
    })
    //显示明文
    //console.log(this.data.youkey)
    wx.showLoading({
      title: '解密中...',
    })
    let that = this
    //key b64
    wx.cloud.callFunction({
      // 云函数名称
      name: 'base64',
      // 传给云函数的参数
      data: {
        flag: 'en',
        str: that.data.youkey,
      },
      success: function (res) {
        //console.log(res.result)
        that.data.youkey = res.result
        //
        // decrypt
        wx.cloud.callFunction({
          name: 'encryption',
          data: {
            flag: 'decrypto',
            str: that.data.text.longtext,
            k: that.data.youkey,
          },
          success: function (res) {
            //console.log(res.result)
            that.data.showtext = res.result
            //
            //text b64
            if (that.data.showtext) {
              wx.cloud.callFunction({
                name: 'base64',
                data: {
                  flag: 'de',
                  str: that.data.showtext,
                },
                success: function (res) {
                  //console.log(res.result)
                  wx.hideLoading()

                  that.data.showtext = res.result
                  that.setData({
                    showtext: that.data.showtext,
                    f: true,
                  })
                },
                fail: function () {
                  wx.hideLoading()
                  wx.showToast({
                    title: '解密失败！',
                    icon: 'none',
                  })
                },
              })
            } else {
              wx.hideLoading()
              wx.showToast({
                title: 'key有误！',
                icon: 'none',
              })

            }
            //text b64 end
          },
          fail: function () {
            wx.hideLoading()
            wx.showToast({
              title: '请求失败！',
              icon: 'none',
            })
            console.error
          }
        })
        //decrypt end
      },
      fail: function () {
        wx.hideLoading()
        wx.showToast({
          title: '请求失败！',
          icon: 'none',
        })
        console.error
      }
    })
    //key b64 end

  },


  //弹框取消
  canceltext: function () {
    this.setData({
      fview: false
    })
  },

  //截取字符
  cutstr: function (s) {
    if (s.length > 26) {
      return s.slice(0, 26);
    } else {
      return s;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      searchword: options.word,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '查询中...',
    })
    let that = this
    const db = wx.cloud.database()
    db.collection('person_memo').where({
      _openid: app.globalData.openid,
      description: that.data.searchword
    }).get({
      success: function (res) {
        //console.log(res.data.length)
        wx.hideLoading()
        if (res.data.length == 0) {
          wx.showToast({
            title: '没有相关内容！',
            icon: 'none',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 500)
        }
        that.data.text.descrip = res.data[0].description
        that.data.text.entext = that.cutstr(res.data[0].encodetext)
        that.data.text.dt = res.data[0].due
        that.data.text.longtext = res.data[0].encodetext

        that.setData({
          text: that.data.text,
        })

      },
      fail: function () {
        wx.hideLoading()
        wx.showToast({
          title: '查询失败！',
          icon: 'none',
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  // onHide: function () {

  // },

  /**
   * 生命周期函数--监听页面卸载
   */
  // onUnload: function () {

  // },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})